import type { PQRequest, PQResponse, PreQuestInstance } from '@prequest/types'
import type { Merge } from 'type-fest'

// error first形式
export type HttpRes<T> =
  | [err: null, res: T]
  | [err: { code?: number, msg?: string }, res: null]

/** 拦截器插件配置 */
type Plugin = Record<string, boolean>
// 扩展PQRequest类型
export type IMRequest = PQRequest & { plugin?: Plugin }
// 扩展PQResponse类型
export type IMResponse = PQResponse & { request: IMRequest }
type Option = Partial<PQRequest & { plugin: Plugin }>
type Method = 'get' | 'post' | 'delete' | 'put' | 'patch' | 'head' | 'options'
type CustomMethodsCallback = {
  // eslint-disable-next-line no-unused-vars
  [k in Method]: <Q>(path: string, option?: Option) => Promise<HttpRes<Q>>
}

export type HttpInstance = Merge<PreQuestInstance, CustomMethodsCallback>
